package com.onstar.cn.weight;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.DecelerateInterpolator;

import com.onstar.cn.common.log.LogManager;

/**
 * Created by 6335yz on 2016/6/21.
 */
public class FuelMeterView extends View {

    Bitmap bitmap;
    Bitmap electBitmap;

    Paint paint;
    RectF rectF;
    float HOME_FUEL_HORIZONTAL;
    float HOME_FUEL_VERTICAL;

    float HOME_FUEL_PROGRESS_HORIZONTAL;
    float HOME_FUEL_PROGRESS_VERTICAL;
    float HOME_FUEL_PROGRESS_HORIZONTAL_MINUS;
    float HOME_FUEL_PROGRESS_VERTICAL_MINUS;
    float HOME_FUEL_ELECTRIC_BOX_HORIZONTAL;
    float HOME_FUEL_ELECTRIC_BOX_VERTICAL;

    float sweep;

    boolean isPhev = true;

    public FuelMeterView(Context context) {
        super(context);
        init();
    }

    public FuelMeterView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    public void playAnimator(float inputSweep) {
        LogManager.getInstance().d("inputSweep === " + inputSweep);
        if (inputSweep > 1 || inputSweep < 0)
            throw new IllegalArgumentException("输入的角度不在表盘区间内");
//        inputSweep = inputSweep * 1.8f;
        ValueAnimator frontEndExtend = ValueAnimator.ofFloat(0, inputSweep);
//        frontEndExtend.setDuration(round_animation / animSteps / 2);
        frontEndExtend.setInterpolator(new DecelerateInterpolator(1));
        frontEndExtend.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                sweep = (Float) animation.getAnimatedValue() * (180);
                invalidate();
            }
        });
        frontEndExtend.start();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int mode = MeasureSpec.getMode(heightMeasureSpec);
        if (mode == MeasureSpec.UNSPECIFIED || mode == MeasureSpec.AT_MOST)
            setMeasuredDimension((int) rectF.width() + (int)getResources().getDimension(R.dimen.home_fuel_margin), (int) rectF.height() / 2 + (int) getResources().getDimension(R.dimen.home_fuel_height));
    }

    private void init() {
        paint = new Paint();
        paint.setColor(getResources().getColor(R.color.color_fuel_progress));
        paint.setStrokeWidth(getResources().getDimension(R.dimen.home_fuel_progress_width));
        paint.setStyle(Paint.Style.STROKE);
        paint.setAlpha(200);
        bitmap = getZoom(BitmapFactory.decodeResource(getResources(), R.drawable.home_fuel_meter), 0.8f, 0.8f);
        electBitmap = getZoom(BitmapFactory.decodeResource(getResources(), R.drawable.home_electric_box), 0.7f, 0.7f);

//        HOME_FUEL_HORIZONTAL = getResources().getDimension(R.dimen.home_fuel_horizontal);
//        HOME_FUEL_VERTICAL = getResources().getDimension(R.dimen.home_fuel_vertical);
//        HOME_FUEL_PROGRESS_HORIZONTAL = getResources().getDimension(R.dimen.home_fuel_progress_horizontal);
//        HOME_FUEL_PROGRESS_VERTICAL = getResources().getDimension(R.dimen.home_fuel_progress_vertical);
//        HOME_FUEL_PROGRESS_HORIZONTAL_MINUS = getResources().getDimension(R.dimen.home_fuel_progress_horizontal_minus);
//        HOME_FUEL_PROGRESS_VERTICAL_MINUS = getResources().getDimension(R.dimen.home_fuel_progress_vertical_minus);
//
//        HOME_FUEL_ELECTRIC_BOX_HORIZONTAL = getResources().getDimension(R.dimen.home_fuel_electric_box_horizontal);
//        HOME_FUEL_ELECTRIC_BOX_VERTICAL = getResources().getDimension(R.dimen.home_fuel_electric_box_vertical);

//        rectF = new RectF(HOME_FUEL_PROGRESS_HORIZONTAL, HOME_FUEL_PROGRESS_VERTICAL, HOME_FUEL_PROGRESS_HORIZONTAL + bitmap.getWidth() - HOME_FUEL_PROGRESS_HORIZONTAL_MINUS, HOME_FUEL_PROGRESS_VERTICAL + bitmap.getHeight() - HOME_FUEL_PROGRESS_VERTICAL_MINUS);
        rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(bitmap, getResources().getDimension(R.dimen.home_fuel_margin), getResources().getDimension(R.dimen.home_fuel_margin), null);
        if (isPhev) {
            canvas.drawBitmap(electBitmap, getMeasuredWidth() / 2 - electBitmap.getWidth() / 2 + getResources().getDimension(R.dimen.home_fuel_ev), getPaddingTop() + bitmap.getWidth() / 4, null);
        }
        RectF rectF1 = new RectF(getPaddingLeft() + getResources().getDimension(R.dimen.home_fuel_margin), getPaddingTop(), getMeasuredWidth() - getPaddingRight(), getMeasuredWidth() - getPaddingBottom());
        canvas.drawArc(rectF1, -180, sweep, false, paint);
    }

    public void updateByCarType(Boolean isPhev) {
        this.isPhev = isPhev;
        invalidate();
    }

    private Bitmap getZoom(Bitmap bitmap, float scaleWidth, float scaleHeight) {
        if (bitmap != null) {
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            Matrix matrix = new Matrix();
            matrix.postScale(scaleWidth, scaleHeight);
            Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
                    matrix, true);
            return newBitmap;
        } else {
            return null;
        }
    }
}
